1855A - Dalton the Teacher - CodeForces Solution


math

Please click on ads to support us..

Python Code:

t=int(input())
for _ in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    s=0
    for i in range(n):
        if l[i]==i+1:
            s+=1
    if s%2==0:
        print(s//2)
    else:
        print((s//2)+1)

C++ Code:

// Author: Tushar Khanduri
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <cmath>
#include <string>
#include <bitset>
#include <algorithm>
#define int long long
#define endl "\n"
#define pb push_back
#define vec vector
using namespace std;

//==============================================================================
class Math
{
public:
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    int lcm(int a, int b)
    {
        return a * (b / gcd(a, b));
    }

    void set_sieve(vector<bool> &sieve, int n)
    {
        for (int i = 2; i * i <= n; i++)
            if (sieve[i])
                for (int j = i * i; j <= n; j += i)
                    sieve[j] = false;
    }

    int pow(int x, int n)
    {
        int ans = 1;
        while (n > 0)
        {
            if (n & 1)
                ans *= x;
            x *= x;
            n = n >> 1;
        }
        return ans;
    }

    int pow(int x, int n, int m)
    {
        int ans = 1;
        while (n > 0)
        {
            if (n & 1)
                ans = (ans * x) % m;
            x = (x * x) % m;
            n = n >> 1;
        }
        return ans;
    }

    int modinv(int x, int p)
    {
        return pow(x, p - 2);
    }

} Math;
//=============================================================================

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for (int &i : a)
        cin >> i;
    int cnt = 0;
    for (int i = 0; i < n; i++)
    {
        cnt += (a[i] == (i + 1));
    }
    cout << (cnt + 1) / 2 << endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tc = 1;
    cin >> tc;
    while (tc--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
1 Comments
  • 12/8/2023 12:23 - Asia/Shanghai

#include <iostream>
using namespace std;

int main(){
    int t,n,x,y = 0;
    cin >> t;
    while(t--){
        cin >> n;
        for(int j = 1;j <= n;j++){
            cin >> x;
            if(x == j) y++;
        }
        cout << (y + 1) / 2 << endl;
    }
    return 0;
}


More Questions

468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions